﻿using Microsoft.AspNetCore.Mvc.ApplicationModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FastNetCore.Framework.Convention
{
    /// <summary>
    /// 自定义路由约定
    /// </summary>
    public class DefaultRouteConvention : IApplicationModelConvention
    {
        private readonly string _defaultTemplate;

        public DefaultRouteConvention(string defaultTemplate)
        {
            _defaultTemplate = defaultTemplate;
        }

        public void Apply(ApplicationModel application)
        {
            // 遍历所有控制器，添加默认路由模板
            foreach (var controller in application.Controllers)
            {
                controller.Selectors.Add(new SelectorModel
                {
                    AttributeRouteModel = new AttributeRouteModel
                    {
                        Template = _defaultTemplate
                    }
                });
            }
        }

    }
}
